<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="650px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <SetCooperation ref="SetCooperation" />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateCooperateSet } from "@/api/facilitator";
import SetCooperation from "@/views/channel/myChannel/setCooperation";
import { isNumber } from '@/utils/is'

export default {
  components: { SetCooperation },
  data() {
    return {
      title: "合作设置",
      open: false,
      form: {
        id: undefined
      },
    };
  },
  methods: {
    openModal(row) {
      this.open = true;
      this.form.id = row.id;
      this.$nextTick(()=>{
        this.$refs.SetCooperation.tcmChannelCooperate.cooperateType = isNumber(row.priceControl) ? row.cooperateType : 1;
        this.$refs.SetCooperation.tcmChannelCooperate.commissionRatio = row.commissionRatio || 0;
        this.$refs.SetCooperation.tcmChannelCooperate.priceControl = isNumber(row.priceControl) ? row.priceControl : 1;
        this.$refs.SetCooperation.tcmChannelCooperate.priceControlLower = row.priceControlLower || 0;
        this.$refs.SetCooperation.tcmChannelCooperate.priceControlUpper = row.priceControlUpper || 0;
      })
    },
    closeModal() {
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    async submitForm() {
      const that = this;
      that.form = { ...that.$refs.SetCooperation.tcmChannelCooperate, ...that.form };
      that.form.priceControl = that.form.cooperateType === 1 ? 1 : that.form.priceControl; // 统一定价返佣,强制设置为控价

      console.log(JSON.parse(JSON.stringify(that.form)));
      await updateCooperateSet(this.form);
      that.$message.success("修改成功");
      that.$emit("refresh");
      that.closeModal();
    },
  },
};
</script>

<style lang="scss" scoped></style>
